//
//  ZHFAuthorDescription.m
//  GoTraveling
//
//  Created by dllo on 15/12/28.
//  Copyright © 2015年 开发华佗. All rights reserved.
//

#import "ZHFAuthorDescription.h"

#import "ZHFAuthorDescripthonCell.h"

@interface ZHFAuthorDescription ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, retain)NSMutableArray *listArray;
@property(nonatomic, retain)UIImageView *imgView;
@property(nonatomic, retain)UILabel *titleLb;

@end

@implementation ZHFAuthorDescription


-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubView];
    }
    return self;
    
}

- (void)createSubView{
    
    //"主播"label
    UILabel *authorLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width / 20, self.frame.size.height /20, self.frame.size.width / 6, self.frame.size.height / 32)];
    authorLabel.text = @"主播:";
    authorLabel.textColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1.0];
    authorLabel.font = [UIFont systemFontOfSize:self.frame.size.height / 32];
    [self addSubview:authorLabel];
    
    //主播头像
    self.authorImage = [[UIImageView alloc]initWithFrame:CGRectMake(80 * ADAPT_WIDTH, 150 * ADAPT_HEIGHT, self.frame.size.width / 10, self.frame.size.width / 10)];
    self.authorImage.center = CGPointMake(authorLabel.frame.origin.x + authorLabel.frame.size.width + self.frame.size.width / 10 / 2, authorLabel.frame.origin.y + authorLabel.frame.size.height / 2);
    self.authorImage.image = [UIImage imageNamed:@"iconfont-zhucedenglutouxiang-2.png"];
    //设为圆形
    self.authorImage.layer.masksToBounds = YES;
    self.authorImage.layer.cornerRadius = self.frame.size.width / 10 / 2;
    [self addSubview:self.authorImage];
    
    //主播昵称
    self.author = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width / 3, authorLabel.frame.origin.y, self.frame.size.width / 3, self.frame.size.height / 32)];
    self.author.text = @"主播昵称";
    self.author.textColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1.0];
    self.author.font = [UIFont systemFontOfSize:self.frame.size.height / 32];
    [self addSubview:self.author];
    
    //"原文"label
    UILabel *textAuthorLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width / 20, authorLabel.frame.origin.y + authorLabel.frame.size.height + self.authorImage.frame.size.height, self.frame.size.width / 6, self.frame.size.height / 32)];
    textAuthorLabel.text = @"原文:";
    textAuthorLabel.textColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1.0];
    textAuthorLabel.font = [UIFont systemFontOfSize:self.frame.size.height / 32];
    [self addSubview:textAuthorLabel];
    
    //原文作者头像
    self.textAuthorImage = [[UIImageView alloc]initWithFrame:CGRectMake(80, 20, self.frame.size.width / 10, self.frame.size.width / 10)];
    self.textAuthorImage.center = CGPointMake(textAuthorLabel.frame.origin.x + textAuthorLabel.frame.size.width + self.frame.size.width / 10 / 2, textAuthorLabel.frame.origin.y + textAuthorLabel.frame.size.height / 2);
    self.textAuthorImage.image = [UIImage imageNamed:@"iconfont-zhucedenglutouxiang-2.png"];
    //设为圆形
    self.textAuthorImage.layer.masksToBounds = YES;
    self.textAuthorImage.layer.cornerRadius = self.frame.size.width / 10 / 2;
    [self addSubview:self.textAuthorImage];
    
    //原文作者昵称
    self.textAuthor = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width / 3, textAuthorLabel.frame.origin.y, self.frame.size.width / 3, self.frame.size.height / 32)];
    self.textAuthor.text = @"原文作者";
    self.textAuthor.textColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1.0];
    self.textAuthor.font = [UIFont systemFontOfSize:self.frame.size.height / 32];
    [self addSubview:self.textAuthor];
    
    //"来自电台"label
    UILabel *fromRadioLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width / 20, textAuthorLabel.frame.origin.y + textAuthorLabel.frame.size.height + self.authorImage.frame.size.height, self.frame.size.width / 5, self.frame.size.height / 32)];
    fromRadioLabel.text = @"来自电台:";
    fromRadioLabel.textColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1.0];
    fromRadioLabel.font = [UIFont systemFontOfSize:self.frame.size.height / 32];
    [self addSubview:fromRadioLabel];
    
    //来自电台
    self.fromRadio = [[UILabel alloc]initWithFrame:CGRectMake(fromRadioLabel.frame.origin.x + fromRadioLabel.frame.size.width, fromRadioLabel.frame.origin.y, self.frame.size.width / 3, fromRadioLabel.frame.size.height)];
    self.fromRadio.textColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1.0];
    self.fromRadio.font = [UIFont systemFontOfSize:self.frame.size.height / 32];
    [self addSubview:self.fromRadio];
    
    //分割线
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(self.frame.size.width / 16, self.frame.size.height / 7 * 3, self.frame.size.width / 16 * 14, 1)];
    line.backgroundColor = [UIColor grayColor];
    [self addSubview:line];
    
    //主播其他作品Label
    UILabel *otherMusic = [[UILabel alloc]initWithFrame:CGRectMake(line.frame.origin.x, self.frame.size.height / 7 * 3, self.frame.size.height / 5, self.frame.size.height / 32)];
    otherMusic.backgroundColor = [UIColor whiteColor];
    otherMusic.text = @"主播其他作品";
    otherMusic.center = CGPointMake(line.frame.origin.x + otherMusic.frame.size.width / 2, line.frame.origin.y);
    otherMusic.textColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1.0];
    otherMusic.font = [UIFont systemFontOfSize:self.frame.size.height / 32];
    [self addSubview:otherMusic];
    
    
    //查看全部电台
    self.checkAllRadio = [UIButton buttonWithType:UIButtonTypeCustom];
    self.checkAllRadio.frame = CGRectMake(self.frame.size.width / 6 * 5, self.frame.size.height / 30 * 28, self.frame.size.height / 30, self.frame.size.height / 30);
    [self.checkAllRadio setImage:[UIImage imageNamed:@"iconfont-qianjin.png"] forState:UIControlStateNormal];
    [self addSubview:self.checkAllRadio];
    
    
    
    //"查看所有电台"Label
    UILabel *checkRadio = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width / 2, self.checkAllRadio.frame.origin.y, self.checkAllRadio.frame.origin.x - self.frame.size.width / 2, self.checkAllRadio.frame.size.height)];
    
    
    //添加下划线
    [checkRadio setLineBreakMode:NSLineBreakByWordWrapping];
    NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"查看所有电台"]];
    NSRange contentRange = {0,[content length]};
    [content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange];
    checkRadio.attributedText = content;
    
    checkRadio.textAlignment = NSTextAlignmentRight;
    checkRadio.font = [UIFont systemFontOfSize:self.frame.size.height / 30];
    checkRadio.textColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1.0];
    [self addSubview:checkRadio];
    
    //创建所有电台UICollectionView
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    //设置每个item(cell)大小
    flowLayout.itemSize = CGSizeMake((self.frame.size.width / 16 * 14 - self.checkAllRadio.frame.size.width * 4) / 3, (self.frame.size.width / 16 * 14 - self.checkAllRadio.frame.size.width * 4) / 3);
    //设置item与四周边界的距离(顺序:上左下右)
    flowLayout.sectionInset = UIEdgeInsetsMake(self.checkAllRadio.frame.size.width, self.checkAllRadio.frame.size.width, self.checkAllRadio.frame.size.width, self.checkAllRadio.frame.size.width);
    
    //collectionView
    self.authorOtherMusic = [[UICollectionView alloc]initWithFrame:CGRectMake(self.frame.size.width / 16, self.frame.size.height / 7 * 3 + self.frame.size.height / 32, self.frame.size.width / 16 * 14, self.frame.size.height / 7 * 3) collectionViewLayout: flowLayout];
    [self addSubview:self.authorOtherMusic];
    self.authorOtherMusic.delegate = self;
    self.authorOtherMusic.dataSource = self;
    self.authorOtherMusic.backgroundColor = [UIColor whiteColor];
    
    //注册
    [self.authorOtherMusic registerClass:[ZHFAuthorDescripthonCell class] forCellWithReuseIdentifier:@"Item"];    
}


#pragma mark - 重写字典set方法
-(void)setAutorDic:(NSMutableDictionary *)autorDic{
    
    
    //其他作品数组
    self.listArray = [NSMutableArray array];
    self.listArray = [autorDic objectForKey:@"moreting"];
    
    //主播赋值
    NSURL *authorImg = [NSURL URLWithString:[[autorDic objectForKey:@"userinfo"] objectForKey:@"icon"]];
    [self.authorImage sd_setImageWithURL:authorImg placeholderImage:nil];
    self.author.text = [[self.autorDic objectForKey:@"userinfo"] objectForKey:@"uname"];
    
    //原文赋值
    NSURL *textAutherImg = [NSURL URLWithString:[[autorDic objectForKey:@"authorinfo"] objectForKey:@"icon"]];
    [self.textAuthorImage sd_setImageWithURL:textAutherImg placeholderImage:nil];
    self.textAuthor.text = [[self.autorDic objectForKey:@"authorinfo"] objectForKey:@"uname"];
    
    //来自电台
    self.fromRadio.text = [autorDic objectForKey:@"radioname"];
    
    //得到值之后刷新
    [self.authorOtherMusic reloadData];
    
}





#pragma mark - 行数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return _listArray.count;
}



#pragma mark - 设置cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ItemIdentifier = @"Item";
    ZHFAuthorDescripthonCell *Item = [collectionView dequeueReusableCellWithReuseIdentifier:ItemIdentifier forIndexPath:indexPath];
    Item.authorDic = [self.listArray objectAtIndex:indexPath.row];
    return Item;
}



#pragma mark - 触摸cell方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString  *tingidStr = [[[self.listArray objectAtIndex:indexPath.row] objectForKey:@"playInfo"] objectForKey:@"tingid"];
    
    // 发送通知 改按钮
    [[NSNotificationCenter defaultCenter] postNotificationName:@"playCollection" object:nil];
    [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"play"];
    [self.authorDeleage sendAuthorDescriptionTingid:tingidStr];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
